var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var url = libs.urlparse(location.href);
var valide = libs.formValide;
var comm = require('../../comm')

//当前页公共变量
var _page = {}




var bindEvent = function(){
    var _ui = SA.get('uiLib')
    if (_ui && _ui==='finish'){
        deal_login()
    }
    else {
        SA.set('uiLib', deal_login)
    }

    function deal_login(){
        function ck_mobil(stat, reg){
            return stat
        }
        function ck_verify(stat, reg){
            return stat
        }

        // 检测是否为数字
        function limit_input_d(str){
            if (str && (!/^[\d]+$/g.test(str))){
                return false;
            }
            return true;
        }

        // 只允许数字
        function only_number(){
            var _val = this.value;
            if (_val && (/[\s|\u4e00-\u9fa5]/g.test(_val))){
                this.value = '';
            }
            else {
                var _last_str = _val.substr(-1)
                if (!limit_input_d(_last_str)){
                    _val = _val.substr(0, (_val.length-1))
                    this.value = _val;
                }
            }
        }


        var kkk = valide('uuuu')
              ('mobile', 'mobile')

        var formBind = valide('xxxx')
              ('mobile', 'mobile', ck_mobil)
              ('validatecode', 'verify_d', ck_verify)
              ("agreement", 'noop', function(stat){
                  if (this.checked){
                      return true
                  }
                  else {
                      return false;
                  }
              })

        $('.login_submit').off('click')
        $('.login_submit').css('backgroundColor','#ccc')

        $('#mobile, #validatecode').on('input', function(){
            only_number.call(this)
            formBind(function(stat, errs){
                if (stat.ckstat){
                    $('.login_submit').removeAttr('style')
                    $('.login_submit').click(_submit)
                }
                else {
                    $('.login_submit').off('click')
                    $('.login_submit').css('backgroundColor','#ccc')
                }
            })
        })

        $('#login_getCode').click(function(){
            var me = this;
            var postdata = kkk(function(stat, errs){
                return stat
            })
            postdata.method = 'get'
            if (postdata.ckstat){
                var _continiue = libs.countDown(me, 30)
                if (_continiue){
                    api.req('/get_mms', postdata, function(data){
                        console.log(data);
                        if (data.code){
                            ui.alertBox({
                                cons: [{text: '验证码不正确', name: ""}],
                                btns: [
                                    {
                                    text: false,
                                    close: true
                                }]
                            })
                        }
                    });
                }
            }
            else {
                ui.alertBox({
                    cons: [{text: '请正确填写手机号码', name: ""}],
                    btns: [
                        {
                        text: false,
                        close: true
                    }]
                })
            }
        });


        // $('.login_submit').click();
        function _submit(){
            var postdata = formBind(function(stat, errs){
                if (errs.mobile){
                    ui.alertBox({
                        cons: [{text: '请正确填写手机', name: ""}],
                        btns: [
                            {
                            text: false,
                            close: true
                        }]
                    })
                }
                else
                if (errs.validatecode){
                    ui.alertBox({
                        cons: [{text: '验证码不正确', name: ""}],
                        btns: [
                            {
                            text: false,
                            close: true
                        }]
                    })
                }
                else
                if (errs.agreement){
                    ui.alertBox({
                        cons: [{text: '请同意大白管家条款', name: ""}],
                        btns: [
                            {
                            text: false,
                            close: true
                        }]
                    })
                }
                return stat
            })
            postdata.openid = _page.openid
            postdata.method = 'get'
            if (postdata.ckstat){
                if (url.params.openid){
                    _page.openid = url.params.openid;
                    var _src = url.source.replace('openid='+url.params.openid, '').replace('?&', '?').replace('?#','#')
                    history.replaceState(null,null, _src)
                }
                api.req('/bind_wx', postdata, function(data, status, xhr){
                    if (data.code===1){
                        // console.log(xhr.getResponseHeader('login'))
                        var _login = xhr.getResponseHeader('login')
                        comm.reState('login', _login)

                        api.req('/userInfo', {login: _login, method: 'get'}, function(ddd){
                            var _user = SA.get('USER')
                            if (ddd.currentCommunity){
                                if (_user.reurl){
                                    var _reurl = _user.reurl;
                                    var _resault = {login: _login, info: ddd}
                                    SA.setter('USER', _resault)
                                    router(_reurl)
                                }
                            }
                            else {
                                var _resault = {login: _login, info: ddd, reurl: _user.reurl}
                                SA.setter('USER', _resault)
                                // router('uc/index')
                                router('uc/autolocation', _resault)
                            }
                        })
                    }
                    else{
                        ui.alertBox({
                            cons: [{text: '验证码不正确', name: ""}],
                            btns: [
                                {
                                text: false,
                                close: true
                            }]
                        })
                    }
                });
            }
        }
        $(".tk").click(function (){
            var htmlstr = "<div class='ab_cover1'>";
            htmlstr += "<div class='mangBox'>";
            htmlstr += '<a href="javascript:;" class="iconfont ab_x"></a>';
            htmlstr += '<div class="clause">';
            htmlstr += '<div class="clauseCon">';
            htmlstr += '<p>快递禁寄物品是指国家法律、法规禁止寄递的物品，主要包括：</p>';
            htmlstr += '<p>(一)各类武器、弹药。如枪支、子弹、炮弹、手榴弹、地雷、炸弹等。</p>';
            htmlstr += '<p>(二)各类易爆炸性物品。如雷管、炸药、火药、鞭炮等。</p>';
            htmlstr += '<p>(三)各类易燃烧性物品，包括液体、气体和固体。如汽油、煤油、桐油、酒精、生漆、柴油、气雾剂、气体打火机、瓦斯气瓶、磷、硫磺、火柴等。</p>';
            htmlstr += '<p>(四)各类易腐蚀性物品。如火硫酸、盐酸、硝酸、有机溶剂、农药、双氧水、危险化学品等。</p>';
            htmlstr += '<p>(五)各类放射性元素及容器。如铀、钴、镭、钚等。</p>';
            htmlstr += '<p>(六)各类烈性毒药。如铊、氰化物、砒霜等。</p>';
            htmlstr += '<p>(七)各类麻醉药物。如鸦片(包括罂粟壳、花、苞、叶)、吗啡、可卡因、海洛因、大麻、冰毒、麻黄素及其它制品等。</p>';
            htmlstr += '<p>(八)各类生化制品和传染性物品。如炭疽、危险性病菌、医药用废弃物等。</p>';
            htmlstr += '<p>(九)各种危害国家安全和社会政治稳定以及淫秽的出版物、宣传品、印刷品等。</p>';
            htmlstr += '<p>(十)各种妨害公共卫生的物品。如尸骨、动物器官、肢体、未经硝制的兽皮、未经药制的兽骨等。</p>';
            htmlstr += '<p>(十一)国家法律、法规、行政规章明令禁止流通、寄递或进出境的物品，如国家秘密文件和资料、国家货币及伪造的货币和有价证券、仿真武器、管制刀具、珍贵文物、濒危野生动物及其制品等。</p>';
            htmlstr += '<p>(十二)包装不妥，可能危害人身安全、污染或者损毁其他寄递件、设备的物品等。</p>';
            htmlstr += '<p>(十三)各寄达国(地区)禁止寄递进口的物品等。</p>';
            htmlstr += '<p>(十四)其他禁止寄递的物品。</p>';
            htmlstr += "</div></div></div></div>";
            $('body').append(htmlstr);
            $('.ab_cover1,.mangBox').off().click(function () {
                $('.mangBox,.ab_cover1').remove();
            });
        })
    }

}

//演示模块
var Show = React.createClass({
    mixins: [ItemMixin],
    render:function(){
        return (
            <div className="default_div app_login">
                <ul className="default_form">
                    <li className="login_li_data">
                        <label>手机号码</label>
                        <input id="mobile" type="text" className="f_phone_code forlogin" />
                        <a id="login_getCode" className="f_code">获取验证码</a>
                    </li>
                    <li className="login_li_data">
                        <label>验证码</label>
                        <input id='validatecode' defaultValue="" type="text" className="forlogin"/>
                    </li>
                </ul>
                <div className="f_ck" style={{padding:'0.14rem 0.16rem 0.2rem 0'}}>
                    <p>
                        <input id="agreement" type="checkbox" defaultChecked className="forlogin"/>我同意<span className="tk">《大白管家条款》</span>
                    </p>
                </div>
                <div className="default_uc_foot" style={{position:'static',backgroundColor:'transparent',marginBottom:"0.2rem",display:"block"}}>
                    <div className="f_submit" style={{width:'100%'}}>
                        <input type="submit" defaultValue="下一步" className="f_btn login_submit"/>
                    </div>
                </div>
            </div>
        );
    }
});

function start(name){
    return pages.new({
        boot: function(){},
        trigger:function(){
            this.libs.changeTitle('用户登录');    //更改当前页面标题
            if (url.params.openid){
                _page.openid = url.params.openid;
                comm.replaceState('openid')
            }
            this.main()
        },
        goback: function(){
            router.cb = function(name){
                if (wx){
                    wx.closeWindow()
                }
            }
        },
        ready: function(){
            this.libs.changeTitle('用户登录');    //更改当前页面标题
        },
        main: function(){
            this.render(
                <Show itemDefaultMethod={bindEvent} />,
                document.getElementById(name)
            )
        }
    })
}

module.exports = start;
